# Get the machine type and kernel name. Piping the output of uname
# # through (tr '[A-Z]' '[a-z]') converts the string to lower case


ARCH := $(shell (uname -m | tr '[A-Z]' '[a-z]'))
OS := $(shell (uname -s | tr '[A-Z]' '[a-z]'))
ARCHOS := $(ARCH)_$(OS)

EXEFILE := Laplacian
BASEDIR := $(HOME)

ifeq ($(ARCHOS),i686_linux)
CC = gcc
CFLAGS = -g -Wall
OCFLAGS = -O -Wall
DEFINES = -DDEBUG -DDEBUG2
endif
ifeq ($(ARCHOS),x86_64_linux)
CC = gcc
CFLAGS = -g -Wall
OCFLAGS = -O -Wall
DEFINES = -DDEBUG -DDEBUG2
endif


DEPS := mstk/globalID-mpi-mstk

INCDIR   := -I./include $(addsuffix /include,$(addprefix -I$(BASEDIR)/,$(DEPS))) \
-I/nh/u/duowang/ACCESS/libraries/exodusii/cbind/include -I/nh/u/duowang/ACCESS/netcdf/netcdf-4.0.1/libsrc
LIBDIR := $(addsuffix /lib/$(ARCHOS),$(addprefix -L$(BASEDIR)/,$(DEPS))) 
#-L/nh/u/duowang/ACCESS/libraries/exodusii/cbind/ -L/nh/u/duowang/ACCESS/netcdf/netcdf-4.0.1/libsrc

LIBS := $(LIBDIR) \
	-lmstk-d 

OPTLIBS := $(LIBDIR) \
	-lmstk

OTHERLIBS := -lm -lpthread /nh/u/duowang/mstk/metis-4.0/libmetis.a /nh/u/duowang/ACCESS/libraries/exodusii/libexoIIv2c.a  /usr/lib/libnetcdf.so.4

OBJ := obj/$(ARCHOS)-d
OPTOBJ := obj/$(ARCHOS)

dir_code:=src
dir_obj:=obj
temp  := $(foreach dir,$(dir_code),$(wildcard $(dir)/*.c))
srcs  := $(notdir $(temp))
objs  := $(srcs:.c=.o)
obj2  := $(addprefix obj/$(ARCHOS)-d/,$(objs))
optobj2  := $(addprefix obj/$(ARCHOS)/,$(objs))
VPATH=src

INSTALLDIR :=.
INSTALLOPTDIR := bin/$(ARCHOS)


debug : $(obj2)
	$(CC) $(CFLAGS) $(LDFLAGS) $(INCDIR) $^ $(LDOBJS) $(LIBS) $(OTHERLIBS)  -o $(INSTALLDIR)/$(EXEFILE)
	@echo
	@echo
	@echo "Executable is $(INSTALLDIR)/$(EXEFILE)"
	@echo "DEBUGGING IS ON"
	@echo

$(OBJ)/%.o : src/%.c
	$(CC) $(DEFINES) $(INCDIR)  $(CFLAGS) $(EXTRACFLAGS) -c $< -o $@

$(obj2) : $(OBJ) $(INSTALLDIR)

$(OBJ) : 
	mkdir -p $(OBJ)	
$(INSTALLDIR):
	mkdir -p $(INSTALLDIR)	

opt : (optobj2)
	$(CC) $(LDFLAGS)  $(INCDIR) $^ $(LDOBJS) $(OPTLIBS) $(OTHERLIBS) -o $(INSTALLOPTDIR)/example 
	@echo
	@echo
	@echo "Executable is $(INSTALLDIR)/example"
	@echo "OPTIMIZED VERSION, DEBUGGING IS OFF"
	@echo


$(OPTOBJ)/%.o : src/%.c
	$(CC) $(DEFINES) $(INCDIR)  $(OCFLAGS) $(EXTRACFLAGS) -c $< -o $@

$(optobj2) : $(OPTOBJ) $(INSTALLOPTDIR)

$(OPTOBJ) :	
	mkdir -p $(OPTOBJ)	
$(INSTALLOPTDIR):
	mkdir -p $(INSTALLOPTDIR)	


clean :
	/bin/rm $(obj2) $(optobj2)
	/bin/rm $(INSTALLDIR) $(INSTALLOPTDIR)
depend:
	makedepend $(INCDIR) $(temp)

# DO NOT DELETE THIS LINE -- make depend depends on it.



